package com.hethongviet.preventingmachine.helps.dialog;

import com.hethongviet.preventingmachine.R;
import com.hethongviet.preventingmachine.helps.SoundPlay;

import android.app.Activity;
import android.app.Dialog;
import android.app.NotificationManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DialogAlarm
{
	private Dialog _dialog = null;

	private TextView _textView = null;

	private ImageView _imageViewError = null;
	private ImageView _imageViewInfo = null;

	private Button _button = null;
	private Button _buttonYES = null;

	private LinearLayout _layoutButtonYes = null;

	private Activity _activity = null;

	/* For button YES */
	private OnClickListener _onClickListenerYes = null;
	private OnClickListener _onClickListenerNo = null;

	public DialogAlarm(Activity activity, boolean isMessageError)
	{
		_activity = activity;

		_dialog = new Dialog(activity);
		_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		_dialog.setContentView(R.layout.dialog_message_confirm);

		_textView = (TextView) _dialog.findViewById(R.id.textDialogMessage);

		_imageViewError = (ImageView) _dialog.findViewById(R.id.image_message_error);
		_imageViewInfo = (ImageView) _dialog.findViewById(R.id.image_message_info);
		if (isMessageError)
		{
			_imageViewError.setVisibility(View.VISIBLE);
			_imageViewInfo.setVisibility(View.GONE);
		}
		else
		{
			_imageViewError.setVisibility(View.GONE);
			_imageViewInfo.setVisibility(View.VISIBLE);
		}

		_button = (Button) _dialog.findViewById(R.id.buttonDialogMessage);
		_buttonYES = (Button) _dialog.findViewById(R.id.buttonDialogMessageYES);
		_layoutButtonYes = (LinearLayout) _dialog.findViewById(R.id.layout_button_dialog_message_yes);
	}

	public DialogAlarm(Activity activity, OnClickListener onClickListenerYES,
			boolean isMessageError)
	{
		this(activity, isMessageError);
		_onClickListenerYes = onClickListenerYES;
	}

	public DialogAlarm(Activity activity, OnClickListener onClickListenerYES,
			OnClickListener onClickListenerNO, boolean isMessageError)
	{
		this(activity, isMessageError);
		_onClickListenerYes = onClickListenerYES;
		_onClickListenerNo = onClickListenerNO;
	}

	public void show(String message, final boolean isFinishActivity, final NotificationManager notificationManager)
	{
		_textView.setText(message);

		if (_onClickListenerNo != null)
		{
			_button.setText(_activity.getString(R.string.no));
			_button.setOnClickListener(_onClickListenerNo);
		}
		else
		{
			_button.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v)
				{
					notificationManager.cancel(0);
					SoundPlay.stop();
					
					_dialog.hide();
					_dialog.dismiss();
					if (isFinishActivity)
					{
						//_dialog.dismiss();

						_dialog = null;
						_activity.finish();
					}
				}
			});
		}

		
		if (_onClickListenerYes != null)
		{
			_buttonYES.setVisibility(View.VISIBLE);
			_layoutButtonYes.setVisibility(View.VISIBLE);
			_buttonYES.setText(_activity.getString(R.string.yes));
			_button.setText(_activity.getString(R.string.no));
			_buttonYES.setOnClickListener(_onClickListenerYes);
		}
		else
		{
			_button.setText(_activity.getString(R.string.back));
			_layoutButtonYes.setVisibility(View.GONE);
			_buttonYES.setVisibility(View.GONE);
		}

		
		if (_dialog != null) _dialog.show();
	}

	public void hide()
	{
		if (_dialog != null) _dialog.hide();
	}
}
